package com.hhh.petadopt.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hhh.petadopt.pojo.Lost;
import com.hhh.petadopt.service.PetLostService;
import com.hhh.petadopt.utils.JsonUtils;

/**
 * 宠物丢了页面展示
 * @author 杜镇声声
 *
 */
@Controller
public class PetLostController {
	
	@Autowired
	private PetLostService petLostService;
	
	@RequestMapping(value="/petLost/showLostTable")
	public String showLostTable() {
		return "lostPet";
	}
	
	//图片上传
		@RequestMapping(value="/petLost/petLostUploadPic")
		@ResponseBody
		public String uploadPic(MultipartFile imgss) throws IllegalStateException, IOException{
			//设置图片名称
			String pname = UUID.randomUUID().toString().replace("-", "");
			//获取原本图片名称
			String originalFilename = imgss.getOriginalFilename();
			//获取图片扩展名
			String extName = FilenameUtils.getExtension(originalFilename);
					
			//将文件存到upload文件夹下
			imgss.transferTo(new File("D:/upload/image/"+pname+"."+extName));
			
			//将图片的名称进行拼接
			String pic = "/pic/"+pname+"."+extName;
			
			String objectToJson = JsonUtils.objectToJson(pic);
			
			//返回拼接好的完整的图片路径
			return objectToJson;
		}
		
		//保存丢失宠物的信息到失物招领表lost表
		@RequestMapping(value="/petLost/savePetLostInfo")
		@ResponseBody
		public String saveLostPetIfo(Lost lost) {
			//执行保存操作
			petLostService.savePetLostInfo(lost);
			
			String str = "成功!";
			return JsonUtils.objectToJson(str);
		}

}
